package com.yzw.controller;

import com.alibaba.excel.util.DateUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author yw
 * @date 2024/7/16
 */
public class FileOutController {

    public void outFile() throws IOException {
        String pre = "INSERT INTO `oms_bc_cloud_active_clear_base` (`active_date`, `cloud_active_order_id`, `active_source_id`, `active_source_type`, `cloud_active_trade_no`, `cloud_product_id`, `device_no`, `device_type`, `noc_order_sku_id`, `noc_order_id`, `card_mark`, `status`) VALUES ";




        //输出txt

        /*String fileName = "C:\\Users\\admin\\Desktop\\云存迁移初始化sql.xls";
        SXSSFWorkbook workbook = new SXSSFWorkbook(1000000);
        workbook.setCompressTempFiles(true);
        Sheet sheetNew = null;
        Row rowNew = null;
        Cell cellNew = null;
        //获得该工作区的第一个sheet
        sheetNew = workbook.createSheet("Sheet0");
        FileOutputStream outStream = new FileOutputStream(fileName);*/

        int pageNum = 1;
        int pageSize = 5000;
        String fileName = "oms_bc_cloud_active_clear_base每次5000条批量导入.txt";
        File file = new File("C:\\Users\\admin\\Desktop\\云存迁移初始化sql\\" + fileName);		//创建指定文件

        if(! file.exists()){
            file.createNewFile();				//如果指定文件不存在，新建文件
        }

        FileWriter fw = new FileWriter(file,true);
        while(true){
            List<OmsBcCloudActiveClearBaseEntity> list = omsBcCloudActiveClearBaseMapper.findByLimit((pageNum - 1) * pageSize, pageSize);
            if(CollectionUtils.isEmpty(list)){
                logger.info("{}全部页执行完毕", pageNum);
                break;
            }

            //fw.write(pre);
            String value = null;
            for (int i = 0; i < list.size(); i++) {
                OmsBcCloudActiveClearBaseEntity omsBcCloudActiveClearBaseEntity = list.get(i);
                if(StringUtils.isEmpty(value)){
                    //fw.write("\r");
                    value = "('" + DateUtils.getFormatStr(omsBcCloudActiveClearBaseEntity.getActiveDate(), DateUtils.yyyyMMddHHmmss) + "'," + omsBcCloudActiveClearBaseEntity.getCloudActiveOrderId() + "," + omsBcCloudActiveClearBaseEntity.getActiveSourceId() + "," + omsBcCloudActiveClearBaseEntity.getActiveSourceType()
                            + ",'" + omsBcCloudActiveClearBaseEntity.getCloudActiveTradeNo() + "'," + omsBcCloudActiveClearBaseEntity.getCloudProductId() + ",'"  + omsBcCloudActiveClearBaseEntity.getDeviceNo()  + "'," + omsBcCloudActiveClearBaseEntity.getDeviceType()
                            + "," + omsBcCloudActiveClearBaseEntity.getNocOrderId() + "," + omsBcCloudActiveClearBaseEntity.getNocOrderSkuId() + ",'" + omsBcCloudActiveClearBaseEntity.getCardMark() + "'," + omsBcCloudActiveClearBaseEntity.getStatus() + ")";
                }
                else {
                    value = value + "," + "('" + DateUtils.getFormatStr(omsBcCloudActiveClearBaseEntity.getActiveDate(), DateUtils.yyyyMMddHHmmss) + "'," + omsBcCloudActiveClearBaseEntity.getCloudActiveOrderId() + "," + omsBcCloudActiveClearBaseEntity.getActiveSourceId() + "," + omsBcCloudActiveClearBaseEntity.getActiveSourceType()
                            + ",'" + omsBcCloudActiveClearBaseEntity.getCloudActiveTradeNo() + "'," + omsBcCloudActiveClearBaseEntity.getCloudProductId() + ",'"  + omsBcCloudActiveClearBaseEntity.getDeviceNo()  + "'," + omsBcCloudActiveClearBaseEntity.getDeviceType()
                            + "," + omsBcCloudActiveClearBaseEntity.getNocOrderId() + "," + omsBcCloudActiveClearBaseEntity.getNocOrderSkuId() + ",'" + omsBcCloudActiveClearBaseEntity.getCardMark() + "'," + omsBcCloudActiveClearBaseEntity.getStatus() + ")";
                }
                //fw.write(value);
            }
            fw.write(pre + value +";");
            // 很重要，不然不会刷到文件中
            fw.write("\r");

            logger.info("第{}页执行完毕", pageNum);
            pageNum ++;
        }
        fw.flush();
    }
}
